package br.edu.universo.si.projeto2.infojob.client.apresentacao;

import br.edu.universo.si.projeto2.infojob.client.componentes.dialogboxes.ErrorDialogBox;
import br.edu.universo.si.projeto2.infojob.client.componentes.dialogboxes.InformationDialogBox;
import br.edu.universo.si.projeto2.infojob.client.componentes.dialogboxes.SuccessDialogBox;
import br.edu.universo.si.projeto2.infojob.client.util.Util;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.Widget;

/*
 * Caixa de Dialogo para alteracao de senha do Profissional ou da Empresa, tanto faz.
 */
public class AAlteraSenha extends DialogBox
{
	private static Image lockIconImage = new Image(Util.getImagensPath() + "lock_icon.png");
	
	private DockPanel dockPanel;
	
	private FlexTable flexTable;

	private PasswordTextBox novaSenhaTxt;
	private PasswordTextBox confirmacaoNovaSenhaTxt;
	private Button alterarSenhaBtn;
	private Button cancelarBtn;
	
	private HorizontalPanel rodape;
	
	public AAlteraSenha(final String conta)
	{
		super();
		
		dockPanel = new DockPanel();
		dockPanel.setSpacing(5);

		this.setText("Alterar senha: " + conta);
		this.setAnimationEnabled(true);

		flexTable = new FlexTable();
		flexTable.setCellPadding(3);

		rodape = new HorizontalPanel();
		rodape.setSpacing(3);

		novaSenhaTxt = new PasswordTextBox();
		confirmacaoNovaSenhaTxt = new PasswordTextBox();
		novaSenhaTxt.setMaxLength(20);
		confirmacaoNovaSenhaTxt.setMaxLength(20);

		alterarSenhaBtn = new Button("Alterar");
		cancelarBtn = new Button("Cancelar");

		rodape.add(alterarSenhaBtn);
		rodape.add(cancelarBtn);
		
		flexTable.setWidget(0, 0, new HTML("<b>Nova senha:</b>"));
		flexTable.setWidget(0, 1, novaSenhaTxt);
		flexTable.getFlexCellFormatter().setAlignment(0, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);

		flexTable.setWidget(1, 0, new HTML("<b>Confirme a nova senha:</b>"));
		flexTable.setWidget(1, 1, confirmacaoNovaSenhaTxt);
		flexTable.getFlexCellFormatter().setAlignment(1, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);
		
		flexTable.setWidget(2, 0, rodape);
		flexTable.getFlexCellFormatter().setColSpan(2, 0, 2);
		flexTable.getFlexCellFormatter().setAlignment(2, 0, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);
		
		dockPanel.add(lockIconImage, DockPanel.WEST);
		dockPanel.add(flexTable, DockPanel.CENTER);
		
		dockPanel.setCellWidth(lockIconImage, "100px");

		this.add(dockPanel);

		// CANCELAR OPERACAO
		cancelarBtn.addClickListener(
			new ClickListener()
			{
				public void onClick(Widget sender)
				{
					AAlteraSenha.this.hide();
				}
			}
		);
		
		// ALTERAR SENHA
		alterarSenhaBtn.addClickListener(
			new ClickListener()
			{
				@SuppressWarnings("unchecked")
				public void onClick(Widget sender)
				{
					if (novaSenhaTxt.getText().equalsIgnoreCase("") || confirmacaoNovaSenhaTxt.getText().equalsIgnoreCase(""))
					{
						InformationDialogBox infoDialogBox = new InformationDialogBox("Preencha todos os campos.");
						infoDialogBox.center();
						infoDialogBox.show();

						return;
					}

					if (!novaSenhaTxt.getText().equals(confirmacaoNovaSenhaTxt.getText()))
					{
						InformationDialogBox infoDialogBox = new InformationDialogBox("Senhas não coincidem.");
						infoDialogBox.center();
						infoDialogBox.show();

						novaSenhaTxt.setText("");
						confirmacaoNovaSenhaTxt.setText("");

						return;
					}

					if (novaSenhaTxt.getText().length() < 6)
					{
						InformationDialogBox infoDialogBox = new InformationDialogBox("A senha deve ter no mínimo 6 caracteres.");
						infoDialogBox.center();
						infoDialogBox.show();

						novaSenhaTxt.setText("");
						confirmacaoNovaSenhaTxt.setText("");

						return;						
					}

					// callback...
					AsyncCallback cbAlterarSenha = new AsyncCallback()
					{
						public void onSuccess(Object result)
						{
							AAlteraSenha.this.hide();
							SuccessDialogBox successDialogBox = new SuccessDialogBox("Senha alterada com sucesso.");
							successDialogBox.center();
							successDialogBox.show();
						};

						public void onFailure(Throwable caught)
						{
							ErrorDialogBox errorDialogBox = new ErrorDialogBox("Erro ao alterar a senha: " + caught.getMessage());
							errorDialogBox.center();
							errorDialogBox.show();
						};
					};
					
					Util.getService().alterarSenha(conta, novaSenhaTxt.getText(), cbAlterarSenha);
				}
			}
		);

	}
}